﻿function Initialization(){
	//速度滑条(不发数据 只改动界面)
	$('#size').on('input propertychange',function(){
	 	data_speed_t=$('#size').val();
        $("#size").css("background-size",data_speed_t+"%"+" "+100+"%");
        $(".data_num").html(data_speed_t+"%");
    });

    // 载入点
	$("#mount_points").on('click',function(){
		$(".ipot_data").css("display","block");
		$(".ipot_data_background").css("display","block");
    });

    // 取消按钮 隐藏 点载入面板
    $("#pot_view_hide").on('click',function(){
		$(".ipot_data").css("display","none");
		$(".ipot_data_background").css("display","none");
    });

    //点击数字按键改变当前的颜色，添加类名
    $(".ipot_data>span").on('click',function(){
		$(this).css("color","#00FFFF").siblings('span').css("color","white");
		$(this).addClass("active_1").siblings('span').removeClass("active_1");
    });

    // 确定按钮 点载入面板
    $("#pot_view_record").on('click',function(){
    	var $act=$(".active_1").html();
    	$(".ipot_data>span").css("color","white");

        if(typeof($act) == "undefined"){
            log("提示:您没有选择点");
        }
        else{
        	var cmd_ok = true;

            // 命名
			var varname = "JP"+$act;
			// 是否array
			// var isArray = $("#new-var-arr").is(":checked");
			// if (isArray)
			// 	varname += "[" + $("#new-var-arr-size").val() + "]";
			// 类型
			var type = "joint";//暂时joint
			// 获取值
			var value = "";
			if (type == "location") {// 笛卡尔点
				var tmp_world = data_point_world;
	            if(tmp_world.length == 6){
	                for(var i=0; i<6; i++){
	                    /* 新方式 */
	                    value += tmp_world[i];
	                    value += ",";
	                }
	                value = value.slice(0,-1);
	            }
	            else{
	            	cmd_ok = false;
	            }
			}
			else if (type == "joint"){// 关节点
				var tmp_joint = data_point_joint;
	            var tmp_joint_len = tmp_joint.length;
	            for(var i=0; i<tmp_joint_len; i++){
	                value += tmp_joint[i];
	                value += ",";
	            }
				value = value.slice(0,-1);
			} 
			else {
				value = "";//其他类型 变量
			}

			if(cmd_ok){
				var cmd = '?TP_ADDVAR("' + varname + '","' + type + '","' + data_login_robotID + '","' + value + '")';
				console.log(cmd); 
				conn.queryMC(cmd, function(result){
					log(result); 
					if (type == "long"){
						// conn.queryMC('?TP_GET_LONGS("")', updateLongs);
					}
					else if (type == "string"){
						// conn.queryMC('?TP_GET_STRINGS("")', updateStrings);
					}
					else if (type == "double"){
						// conn.queryMC('?TP_GET_DOUBLES("")', updateDoubles);
					}
					else if (type == "location" || type == "joint"){
						conn.queryMC('?TP_get_locations("all")', function(locationsString){
							console.log("locations:"+locationsString);
							// if (locationsString == "")
							// 	locations = [];
							// else
							// 	locations = locationsString.split(",");
								
							conn.queryMC('?TP_GET_JOINTS("ALL")', function(jointsString){
								console.log("joint:"+jointsString);
								// if (jointsString == "")
								// 	joints = [];
								// else
								// 	joints = jointsString.split(",");
									
								// if (currFrame != null) {
								// 	updatePanelsAfterFrameSelect();
								// }
								// updateDataTable();
								
							});
							
						});
					}
					//刷新
					// conn.queryMC('?TP_GET_TOOL_LIST("")', updateTools);
					// conn.queryMC('?TP_GET_BASE_LIST("")', updateBases);
					// $("input[Name='data-table-type']").trigger("change");
				});
			}
			else{
				log("record point failed:cmd_ok=false");
			}
            

            // 存入全局变量
            // $activeData[$act-1] = $active;

            $(".ipot_data").css("display","none");
            $(".ipot_data_background").css("display","none");
            $(".active_1").removeClass("active_1");
        }
        
    });
}
Initialization();

// 修改速度
function change_speed(){
    var value = document.getElementById('size').value ;
    console.log(value)

    if(conn && data_login){
        conn.queryMC('?TP_SPEED(' + value + ')',null);
    }
}

// 寸动按钮
function touch_start(sub1,sub2){
	$(sub1).on("touchstart",function(){
	vid = setInterval(function(){
		conn.queryMC('?tp_jog(' + sub2 + ')', handleError);
		},150);
	});

	$(sub1).on("touchend",function(){
		conn.queryMC("?tp_jog(0)", handleError);
		clearInterval(vid);
		// console.log("tp_jog(0)");
	});
}
touch_start("#jog_l1","-1");
touch_start("#jog_r1","1");
touch_start("#jog_l2","-2");
touch_start("#jog_r2","2");
touch_start("#jog_l3","-3");
touch_start("#jog_r3","3");
touch_start("#jog_l4","-4");
touch_start("#jog_r4","4");
touch_start("#jog_l5","-5");
touch_start("#jog_r5","5");
touch_start("#jog_l6","-6");
touch_start("#jog_r6","6");












